package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.QqpayConfigMapper;
import com.so206.po.QqpayConfig;
import com.so206.service.QQPayConfigService;
import com.so206.utils.BeanCheck;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class QQPayConfigServiceImpl implements QQPayConfigService {

    @Autowired
    private QqpayConfigMapper mapper;

    @Override
    public PageInfo<QqpayConfig> find_by_page(Integer page, Integer rows, Integer status, String name, String mchid) {
        PageHelper.startPage(page, rows);
        if (StringUtils.isEmpty(name)) {
            name = null;
        }
        if (StringUtils.isEmpty(mchid)) {
            mchid = null;
        }
        List<QqpayConfig> list = mapper.selectByStatusNamePid(status, name, mchid);
        return new PageInfo<>(list);
    }

    @Override
    public Long save(QqpayConfig config) {
        config.setCreateTime(new Date());
        config.setOptTime(new Date());
        mapper.insert(config);
        return config.getId();
    }

    @Override
    public QqpayConfig find_by_id(Long id) {
        return mapper.selectByPrimaryKey(id);

    }

    @Override
    public int update(QqpayConfig config) {
        QqpayConfig model = mapper.selectByPrimaryKey(config.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(config, model);
            model.setOptTime(new Date());
            return mapper.updateByPrimaryKeySelective(model);
        }
        return 0;
    }

    @Override
    public void delete_by_id(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<QqpayConfig> find_all_conn(Integer status) {
        return mapper.selectByStatusNamePid(status, null, null);
    }


}
